//
//  MoviePlayerViewController.m
//  SHIPinPlayer
//
//  Created by lanou3g on 15/10/19.
//  Copyright (c) 2015年 张国文. All rights reserved.
//

#import "MoviePlayerViewController.h"
#import <MediaPlayer/MediaPlayer.h>

@interface MoviePlayerViewController ()

@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;

@end

@implementation MoviePlayerViewController

- (MPMoviePlayerController *)moviePlayer
{
    if (!_moviePlayer) {
        // 负责控制媒体播放的控制器
     
        _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:self.movieURL];
        
        // 自动调整大小
        _moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _moviePlayer.view.frame = self.view.bounds;
        [self.view addSubview:_moviePlayer.view];
    }
    return _moviePlayer;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 视频播放
    [self.moviePlayer play];
    
    NSLog(@"viewDidLoad加载完成");
    
    
}

- (void)viewDidAppear:(BOOL)animated
{
    // 全屏
    // 添加通知
    [self addNotification];
    self.moviePlayer.fullscreen = YES;
   // NSLog(@"viewDidAppear");
}

#pragma mark - 添加通知
- (void)addNotification
{
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    // 1、添加播放状态的监听
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stateChanged) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
    
    // 2、播放完成
    [nc addObserver:self selector:@selector(finished) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    // 3、退出全屏(按done)
    [nc addObserver:self selector:@selector(finished) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
    // 4、截屏
    [nc addObserver:self selector:@selector(captureFinished:) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:nil];
  
    /**
   MPMoviePlayerWillEnterFullscreenNotification
   MPMoviePlayerDidEnterFullscreenNotification
   MPMoviePlayerWillExitFullscreenNotification
   MPMoviePlayerDidExitFullscreenNotification
     */
    
    
    
}
/*
 MPMoviePlaybackStateStopped, 停止
 MPMoviePlaybackStatePlaying, 播放
 MPMoviePlaybackStatePaused,  暂停
 MPMoviePlaybackStateInterrupted, 中断
 MPMoviePlaybackStateSeekingForward, 下一个
 MPMoviePlaybackStateSeekingBackward 上一个
 */

// 截取指定时间的图像
- (void)captureImageAtTime:(float)time
{
    // 数组中有多少时间，就通知几次
    // MPMovieTimeOptionExact 精确
    // MPMovieTimeOptionNearestKeyFrame 大概
    [self.moviePlayer requestThumbnailImagesAtTimes:@[@(time)] timeOption:MPMovieTimeOptionExact];
}


- (void)captureFinished:(NSNotification *)notification
{
 //   NSLog(@"notification = %@", notification);
    [self.delegate moviePlayerDidCaptureWithImage:notification.userInfo[MPMoviePlayerThumbnailImageKey]];
}


- (void)stateChanged
{
    switch (self.moviePlayer.playbackState) {
        case MPMoviePlaybackStatePlaying:
            NSLog(@"播放");
            break;
            case MPMoviePlaybackStatePaused:
            NSLog(@"暂停");
            break;
            case MPMoviePlaybackStateStopped:
            // 执行[self.moviePlayer stop]或者前进后退不工作时会触发
            NSLog(@"停止");
            break;
        default:
            break;
    }
    
    
}

- (void)finished
{
    // 1、删除通知监听
    [[NSNotificationCenter defaultCenter] removeObserver:self];
   
    
    // 运用代理返回
    [_delegate moviePlayerDidFinished];

    NSLog(@"完成");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
